package com.zyl.courseservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyl.commonbase.constant.UserConstant;
import com.zyl.commonbase.handle.CourseException;
import com.zyl.commonutils.ResponseResult;
import com.zyl.courseservice.entity.TUser;
import com.zyl.courseservice.entity.vo.StudentVo;
import com.zyl.courseservice.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Auther:郑誉略
 * Date: 2020-12-08 10:20
 * Description: <描述>
 */
@Api( description = "学生管理")
@RestController
@RequestMapping({"/student"})
@CrossOrigin
public class StudentController {

    private TUserService userService;
    public StudentController(TUserService userService) {
        this.userService = userService;
    }

    @Autowired
    private TGradeService gradeService;

    @Autowired
    private TCollegeService collegeService;

    @Autowired
    private TMajorService majorService;

    @Autowired
    private TClassesService classesService;

    @ApiOperation("所有学院" )
    @GetMapping({"getCollege"})
    public ResponseResult getCollege() {
        List list = this.collegeService.list(null);
        return ResponseResult.ok().data("items", list);
    }

    @ApiOperation("专业信息" )
    @GetMapping({"/getMajor/{value}"})
    public ResponseResult getMajor(@PathVariable int value) {
        QueryWrapper wrapper = new QueryWrapper();
        System.out.println(value);
        wrapper.eq("college_id", value);
        List list = this.majorService.list(wrapper);
        return ResponseResult.ok().data("items", list);
    }

    @ApiOperation("班级信息" )
    @GetMapping({"/getClasses/{value}"})
    public ResponseResult getClasses(@PathVariable int value) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("major_id", value);
        List list = this.classesService.list(wrapper);
        return ResponseResult.ok().data("items", list);
    }


    @ApiOperation("所有年级" )
    @GetMapping({"getGrade"})
    public ResponseResult getGrade() {
        List list = this.gradeService.list(null);
        return ResponseResult.ok().data("items", list);
    }

    @ApiOperation("所有学生列表")
    @GetMapping({"findAll"})
    public ResponseResult findAllStudent() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type", UserConstant.STUDEND_DEFAULT);
        List list = this.userService.list(wrapper);
        return ResponseResult.ok().data("items", list);
    }

    @ApiOperation("分页查询学生列表")
    @GetMapping({"pageStudent/{current}/{limit}"})
    public ResponseResult pageStudent(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody( required = false ) TUser student) {
        Page pageStudent = new Page(current, limit);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type", UserConstant.STUDEND_DEFAULT);
        this.userService.page(pageStudent, wrapper);
        long total = pageStudent.getTotal();
        List records = pageStudent.getRecords();
        return ResponseResult.ok().data("total", Long.valueOf(total)).data("rows", records);
    }

    @ApiOperation("分页查询学生所有信息")
    @GetMapping({"pageStudentAll/{current}/{limit}"})
    public ResponseResult pageStudentAll(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody( required = false ) TUser student) {
        Page pageStudent = new Page(current, limit);
        Page<StudentVo> studentList = userService.getStudentList(pageStudent);
        long total = pageStudent.getTotal();
        List records = pageStudent.getRecords();
        return ResponseResult.ok().data("total", Long.valueOf(total)).data("rows", records).data("student",studentList);
    }

    @ApiOperation("新增学生")
    @PostMapping({"/addStudent"})
    public ResponseResult addStudent(@RequestBody TUser student) throws CourseException {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("stunum", student.getStunum());
        TUser t = (TUser)this.userService.getOne(wrapper);
        if(null != t) {
            System.out.println(t);
            throw new CourseException(20001, "该学号已存在");
        } else {
            student.setType(UserConstant.STUDEND_DEFAULT);
            boolean save = this.userService.save(student);
            return save? ResponseResult.ok(): ResponseResult.error();
        }
    }

    @ApiOperation("根据ID删除学生")
    @DeleteMapping({"{id}"})
    public ResponseResult removeById(@ApiParam(
            name = "id",
            value = "学生ID",
            required = true
    ) @PathVariable int id) {
        boolean flag = this.userService.removeById(id);
        return flag? ResponseResult.ok(): ResponseResult.error();
    }

    @ApiOperation("修改学生信息")
    @PutMapping("{id}")
    public ResponseResult updateStduent(
            @PathVariable int id,
            @RequestBody TUser student){
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("stunum",student.getStunum());
        TUser t = userService.getOne(wrapper);
        if(null != t) {
            System.out.println(t);
            throw new CourseException(20001, "该学号已存在");
        } else {
            student.setType(UserConstant.STUDEND_DEFAULT);
            student.setUserId(id);
            userService.updateById(student);
            return ResponseResult.ok();
        }
    }


}
